package com.enniswu.weitter.user.start;

import com.enniswu.weitter.base.BaseModel;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.JsonBean;
import com.enniswu.weitter.network.HttpClient;
import com.enniswu.weitter.util.MD5Util;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

import static com.enniswu.weitter.entity.JsonBean.TypeStore.intType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.objType;

public class StartModel extends BaseModel {

    public static final int TYPE_LOGIN = 0;
    public static final int TYPE_REGISTER = 1;

    public void loginOrRegister(int type, String phone, String psw, Callback<Integer> callback) {
        Map<String, String> params = new HashMap<>(3, 1);
        params.put("userName", phone);
        params.put("password", MD5Util.parseStrToMd5U16(psw));
        params.put("userType", String.valueOf(1));
        String url;
        switch (type) {
            case TYPE_LOGIN:
                url = HttpClient.URLStore.LOGIN;
                break;
            case TYPE_REGISTER:
                url = HttpClient.URLStore.REGISTER;
                break;
            default:
                throw new IllegalArgumentException();
        }
        HttpClient.getInstance().post(null, url,
                params, new HttpCallbackImpl<Integer>(callback, intType) {
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String json = handleResponse(response);
                        if (json == null)
                            return;
                        String sessionId = catchSessionId(response);
                        if (sessionId == null)
                            back.onFailure(HttpClient.getErrorMessage());
                        SPSupport.getInstance().putSessionId(sessionId);
                        JsonBean<Integer> jsonBean = new Gson().fromJson(json,
                               intType);
                        back.onSuccess(jsonBean.getT());
                    }
                });
    }

    public void sendCode(String phone, Callback<Object> callback) {
        Map<String, String> params = new HashMap<>(1, 1);
        params.put("phone", phone);
        HttpClient.getInstance().post(null, HttpClient.URLStore.CODE_SEND,
                params, new HttpCallbackImpl<Object>(callback, objType) {
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (handleResponse(response) == null)
                            return;
                        String sessionId = catchSessionId(response);
                        if (sessionId == null)
                            back.onFailure(HttpClient.getErrorMessage());
                        SPSupport.getInstance().putSessionId(sessionId);
                        back.onSuccess(null);
                    }
                });
    }

    public void authCode(String phone, String code, String fromTime, Callback<Object> callback) {
        Map<String, String> params = new HashMap<>(3, 1);
        params.put("phone", phone);
        params.put("code", code);
        params.put("fromTime", fromTime);
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.CODE_AUTH, params, new HttpCallbackImpl<>(callback, objType));
    }
}
